/**macd 形态
 * 快慢线零轴线以上，回踩零轴但不下零轴（或者破一点点，macd绿柱极小），是构筑第二买点。 两根线都贴近零轴是最好的，离零轴太高，可能会继续下跌
 * **/
create or replace function getstdif(days int) returns setof record as
$$
declare
rec record;
begin
  for rec in EXECUTE '
	select b."date",b.code,b.macd,
		s."name",s.industry from basic_data b  left join stocks s  on b.code=s.code 
	where 
	to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days||')
		and s."timeToMarket" <>0 and to_date(to_char(s."timeToMarket",''99999999''),''YYYYMMDD'') < current_date-180
		and strpos(s."name",''ST'')=0
		and dif>dea
		and dif>0
		and strpos(s."name",''ST'')=0
		and dif >= (select dif from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+1||') and code=b.code )
		and (select min(dif) from basic_data where to_date(date,''YYYY-MM-DD'') <= getwd(current_date,'||days+1||') and to_date(date,''YYYY-MM-DD'') >= getwd(current_date,'||days+8||') and code=b.code ) <= (select min(dif) from basic_data where to_date(date,''YYYY-MM-DD'') >= getwd(current_date,'||days+21||') and to_date(date,''YYYY-MM-DD'') <= getwd(current_date,'||days||') and code=b.code )
		--and 0 < (select dif from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days||') and code=b.code )
		order by macd::float8		
;
	' loop
    return next rec;
  end loop;
return;
end
$$
language 'plpgsql';